package com.ntschy.web.controller.sys;

import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.entity.dataobject.SysMenu;
import com.ntschy.common.core.entity.dataobject.SysUser;
import com.ntschy.common.core.entity.vo.RouterVo;
import com.ntschy.common.core.model.LoginBody;
import com.ntschy.common.core.model.LoginUser;
import com.ntschy.common.utils.ServletUtils;
import com.ntschy.framework.service.SysLoginService;
import com.ntschy.framework.service.SysPermissionService;
import com.ntschy.framework.service.TokenService;
import com.ntschy.sys.service.ISysMenuService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ezreal_geng
 */
@Api(tags = "登录接口")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     */
    @ApiOperation(value = "pc端登录", notes = "")
    @ApiImplicitParam(name = "loginBody", value = "pc用户登录", required = true, dataType = "LoginBody")
    @ApiResponse(code = 200, message = "登录成功", response = String.class )
    @PostMapping("/login")
    public AjaxResult<String> login(@Validated @RequestBody LoginBody loginBody) {
//        String decryptPwd = null;
//        try {
//            decryptPwd = EncryptUtil.decryptRSADefault(loginBody.getPassword());
//        } catch (Exception e) {
//            throw new CustomException("用户名密码解密失败");
//        }
//        loginBody.setPassword(decryptPwd);
//        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword());
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword());
        return AjaxResult.success("登录成功1", token);
    }




    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation(value = "获取用户信息", notes = "")
    @ApiResponses(@ApiResponse(code = 200, message = "查询成功" ))
    @GetMapping("/getInfo")
    public AjaxResult<Map<String, Object>> getInfo() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        Map<String, Object> data = new HashMap<>();
        data.put("user", user);
        data.put("roles", roles);
        data.put("permissions", permissions);
        return AjaxResult.success("获取用户信息成功", data);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation(value = "获取路由信息", notes = "")
    @ApiResponses(@ApiResponse(code = 200, message = "获取路由成功" ))
    @GetMapping("/getRouters")
    public AjaxResult<List<RouterVo>> getRouters() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.listMenuTreeByUserId(user.getId());
        return AjaxResult.success("获取路由信息成功", menuService.buildMenus(menus));
    }
}
